package com.baizhi.controller;

import com.baizhi.entity.CmfzArticle;
import com.baizhi.service.CmfzArticleService;
import com.baizhi.service.LuceneService;
import org.apache.lucene.search.highlight.InvalidTokenOffsetsException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2019/4/9 0009.
 */
@Controller
@RequestMapping("/article")
public class CmfzArticleController {
    @Autowired
    private CmfzArticleService cmfzArticleService;
    @Autowired
    private LuceneService luceneService;

    @RequestMapping("/showAll")
    public @ResponseBody
    Map showAll(Integer page, Integer rows,String articleKeywords) {
        Map map = null;
        try {
            map = cmfzArticleService.showAll(page, rows,articleKeywords);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InvalidTokenOffsetsException e) {
            e.printStackTrace();
        }
        return map;
    }

    @RequestMapping("/add")
    public @ResponseBody Map add(MultipartFile picture, CmfzArticle cmfzArticle, HttpServletRequest request) {
        Map add = cmfzArticleService.add(picture, cmfzArticle, request);
        return add;
    }

    @RequestMapping("resetIndexDB")
    public @ResponseBody  Map resetIndexDB(){
        HashMap<String, Boolean> map = new HashMap<>();
        boolean b = luceneService.resetIndexDB();
        map.put("flag",b);
        return map;
    }

}
